package com.sura.deployment.svn;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.*;

import org.tmatesoft.svn.core.wc.*;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;




import com.sura.deployment.xls.Session;


public class SVNManager {
	private String pathRepository;
	private String dstPath;
	private String usser;
	private String password;
	private SVNClientManager clientManager;
	SVNUpdateClient myClient;
	SVNDiffClient diffClient;
	SVNCopyClient copyClient;
	
	public SVNManager(){
		this.pathRepository = "http://inmdecvd01.suranet.com:4747/serviciosDeSeguros/Osiris/Prueba/";
		this.dstPath = "C:/WINDOWS/Temp/"; 
		this.usser = "henrpiar";
		this.password = "healpiar50766";
		DAVRepositoryFactory.setup();
		clientManager = SVNClientManager.newInstance(SVNWCUtil.createDefaultOptions(true), usser, password);
		myClient = clientManager.getUpdateClient();
		diffClient = clientManager.getDiffClient();
		copyClient = clientManager.getCopyClient();
	}
	
	public void doCheckoutTrunk()throws SVNException{
		SVNURL urlTrunk = SVNURL.parseURIEncoded(pathRepository + "trunk/");
		SVNURL urlFonts = SVNURL.parseURIEncoded(pathRepository + "branches/NewFonts/");
		File wcDir = new File(dstPath + "DeployFolder");
		File wcDirTrunk = new File(dstPath + "DeployFolder/CheckoutTrunk/");
		File wcDirFonts = new File(dstPath + "DeployFolder/NewFonts/");
		
		if(wcDir.exists())
			wcDir.delete();
		wcDir.mkdir();

		
		myClient.setIgnoreExternals( false );
		
		try{
			myClient.doCheckout(urlTrunk, wcDirTrunk, SVNRevision.HEAD, SVNRevision.HEAD,  SVNDepth.UNKNOWN, true);
		}catch(SVNException e){
			myClient.doCheckout(urlTrunk, wcDirTrunk, SVNRevision.HEAD, SVNRevision.HEAD,  SVNDepth.UNKNOWN, true);
		}
		
		try{
			myClient.doCheckout(urlFonts, wcDirFonts, SVNRevision.HEAD, SVNRevision.HEAD,  SVNDepth.UNKNOWN, true);
		}catch(SVNException e){
			myClient.doCheckout(urlFonts, wcDirFonts, SVNRevision.HEAD, SVNRevision.HEAD,  SVNDepth.UNKNOWN, true);
		}
	}
	
	public void doMergeTrunk()throws SVNException, IOException{
		List copySource  = new ArrayList();
		ArrayList<String> copy  = new ArrayList<String>();
		SVNRevisionRange rangeToMerge = new SVNRevisionRange(SVNRevision.create(1), SVNRevision.HEAD);
		StatusHandler statusHandler = new StatusHandler();
		
		String[][] changedElements = Session.getChangedElements();
		int countFiles = changedElements.length;
		
		for(int i=0; i<countFiles; i++){
			String url = pathRepository + "branches/" + changedElements[i][5] + changedElements[i][3];
			if(changedElements[i][2].equals("Cambio")){
				File pathFile = new File(dstPath +  "DeployFolder/CheckoutTrunk/" + changedElements[i][3]);
				while(true){
					try {
						diffClient.doMerge(SVNURL.parseURIEncoded(url), SVNRevision.HEAD, Collections.singleton(rangeToMerge), 
											pathFile, SVNDepth.UNKNOWN, true, false, false, false);
						break;
					}catch (SVNException e){
						System.out.println(e.getMessage());
					}
				}
			}else if (changedElements[i][2].equals("Nuevo")){
				copySource.add(new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, SVNURL.parseURIEncoded(url)));
				copy.add(changedElements[i][3]);
			}else
				new File(dstPath + "DeployFolder/CheckoutTrunk/" + changedElements[i][3]).delete();
		}
		
		if(copySource.size()>0)
			doCopy(copySource,copy);

		clientManager.getStatusClient().doStatus(new File(dstPath+"DeployFolder/CheckoutTrunk/"), SVNRevision.HEAD, SVNDepth.INFINITY, 
														false, false, false,false, statusHandler, null);
		statusHandler.getStatus();
	}
	
	public void doCopy(List copySource,ArrayList<String> copy)throws SVNException,IOException{ 
		SVNCopySource[] source = new SVNCopySource[copySource.size()];
		
		for(int i=0; i<copySource.size(); i++)
			source[i] = (SVNCopySource)copySource.get(i);
			
		while(true){
			try {
				copyClient.doCopy(source, new File(dstPath + "DeployFolder/NewFonts/"), false, false, false);
				break;
			}catch (SVNException e){
				System.out.println(e.getMessage());
			}
		}
			
		for(int i=0; i<copySource.size(); i++){
			String font = copy.get(i).substring((copy.get(i).lastIndexOf('/')));
			font = dstPath + "DeployFolder/NewFonts/" + font;
			copyFile(new File(font),new File(dstPath + "DeployFolder/CheckoutTrunk" + copy.get(i)));
		}
	}
	
	public void copyFile(File sourceFile, File destFile) throws IOException { 
		   FileChannel source = new FileInputStream(sourceFile).getChannel(); 
		   FileChannel destination = new FileOutputStream(destFile).getChannel(); 
		   destination.transferFrom(source, 0, source.size()); 
		   source.close(); 
		   destination.close(); 
	}

	
}
